package cn.skyclass.controls
{
	import cn.skyclass.events.EventNotification;
	import cn.skyclass.events.PopUpEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.MoveEvent;
	import mx.events.ResizeEvent;	
	
	/**
	 * 使用一个画布来模拟外部窗口，外部窗口由 iframe 来实现
	 */    		
	public class OuterWindow extends Canvas
	{
		/**
		 * 用于监听 PopUpEvent 事件的全局监听器实例名称
		 */
		public var enInstanceName:String = EventNotification.DEFAULT_INSTANCE_NAME;
		protected function get en():EventNotification{
			return EventNotification.getInstance(this.enInstanceName);
		}
 		
		// 弹出窗口的数目
		private var popCount:int=0;
        
        // 在弹出窗口期间，此控件自身被设置隐藏次数（不受弹出窗口控制的部分）
        private var selfHideCount:int=0;
        private var innerHide:Boolean=false;
        
        // 记录在弹出窗口之前的可视状态
        private var visibilityBeforePop:Boolean;
		
		public function OuterWindow(){
			this.addEventListener(FlexEvent.CREATION_COMPLETE, this.creationCompleteHandler);
			this.addEventListener(ResizeEvent.RESIZE, this.moveHandler);
			this.addEventListener(MoveEvent.MOVE, this.moveHandler);
						
			this.visibilityBeforePop = this.visible;
		}
							
		private function creationCompleteHandler(e:Event=null):void{
			// 监听弹出窗口事件，目的是在有弹出窗口的时候把外部的网页暂时隐藏，
			// 避免将弹出窗口遮住，使得用户无法继续操作
			this.en.addEventListener(PopUpEvent.POP_UP_OPEN, this.popOpenHandler);
			this.en.addEventListener(PopUpEvent.POP_UP_CLOSE, this.popCloseHandler);
			
			// 在所有的父控件上注册相关事件监听器
			var app:Object = this.parentApplication;
			var pr:DisplayObjectContainer = this.parent;
			this.registerHandler(app);
			while((pr != null) && (pr != app)){
				this.registerHandler(pr);
				pr = pr.parent;
			}	
		}
				
		private function popOpenHandler(e:Event):void{			
			this.popCount++;
			
			// 第一次弹出窗口的时候，记录下状态
			if(this.popCount == 1){
				this.visibilityBeforePop = this.visible;
			}
			this.innerHide = true;
			this.visible = false;
			this.innerHide = false;
		}
		
		private function popCloseHandler(e:Event):void{
			this.popCount--;		
			if(this.popCount <= 0){
				this.popCount = 0;
			}	
				
			// 弹出窗口全部消失，并且弹出之前是可视的，并且没有通过自身或其他方式隐藏，则可以将其再次显示出来
			if(this.popCount == 0 && this.visibilityBeforePop && this.selfHideCount <= 0){
				this.visible = true;
			}
		}
		
		override public function get visible():Boolean{
			return super.visible;
		}		
		override public function set visible(visible:Boolean):void{
			super.visible = visible;
			
			// 设置可视性的时候，有弹出窗口情况下，记录自身改变的状态			
			if(this.popCount > 0 && !this.innerHide){
				this.selfHideCount = this.selfHideCount + (visible ? -1 : 1);
			}
		}
		
		private function registerHandler(ui:Object):void{
			// 注册 hide, remove 事件监听器，当父控件被隐藏或是移除之时，自动隐藏自身
			// 注册 move 事件监听器，使得所有父控件移动时，自己跟着移动			
			if(ui is UIComponent){
				UIComponent(ui).addEventListener(FlexEvent.HIDE, this.hideHandler);
				UIComponent(ui).addEventListener(FlexEvent.REMOVE, this.hideHandler);
				UIComponent(ui).addEventListener(MoveEvent.MOVE, this.moveHandler);
			}				
		}
		
		private function hideHandler(e:Event):void{
			this.visible = false;
		}
		
		private function moveHandler(e:Event):void{
			callLater(this.moveOuterWindow);
		}
		
		/**
		 * 移动外部窗口的操作，使得 iframe 跟着内部的 canvas 移动
		 */
        protected function moveOuterWindow(e:Event=null):void{
        	// do nothing
        }	
			
	}
}